# 5.14 Multiple Data Source Support

JFinal's ActiveRecordPlugin can simultaneously support multiple data sources, dialects, caches, and transaction levels. Each ActiveRecordPlugin can be configured independently, allowing JFinal to work with multiple data sources while also supporting independent configuration for dialects, caches, transaction levels, etc.

# Configuring Multiple Data Sources

When using multiple data sources, you need to specify a configName for each ActiveRecordPlugin. Here's a code example:

public void configPlugin(Plugins me) {
  // MySQL data source
  DruidPlugin dsMysql = new DruidPlugin(...);
  me.add(dsMysql);

  // MySQL ActiveRecordPlugin instance, specifying configName as "mysql"
  ActiveRecordPlugin arpMysql = new ActiveRecordPlugin("mysql", dsMysql);
  me.add(arpMysql);
  arpMysql.addMapping("user", User.class);

  // Oracle data source
  DruidPlugin dsOracle = new DruidPlugin(...);
  me.add(dsOracle);

  // Oracle ActiveRecordPlugin instance, specifying configName as "oracle"
  ActiveRecordPlugin arpOracle = new ActiveRecordPlugin("oracle", dsOracle);
  me.add(arpOracle);
  arpOracle.setDialect(new OracleDialect());
  arpOracle.addMapping("blog", Blog.class);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

In the above code, two ActiveRecordPlugin instances (arpMysql and arpOracle) are created. Note that their configNames are specified as "mysql" and "oracle" respectively. Different Models and dialects are mapped and configured for each instance.

# Switching Data Sources for Models

Different Models will automatically find their respective ActiveRecordPlugin instances and related configurations for database operations. If you wish to switch a single Model to different data sources, it's also straightforward. This is particularly useful when different data sources have tables with the same structure, and you want to use the same Model for these tables. Here's an example:

public void multiDsModel() {
  // Use the data source associated with arp.addMapping(...) by default
  Blog blog = new Blog().dao().findById(123);

  // Switch to another data source by calling the use method once
  blog.use("mysql-bak").save();
}
1
2
3
4
5
6
7

In the above code, blog.use("mysql-bak") switches the data source to "mysql-bak," and the subsequent save() method will save the data to that data source.

Note: The use method is only necessary if the same Model needs to correspond to tables from multiple data sources. If a Model uniquely corresponds to a table in a single data source, the switch is automatic, and there's no need for the use method.

# Switching Data Sources for Db + Record

For Db + Record usage, switching data sources requires using the Db.use(configName) method to get the database operation object. Then you can proceed with database operations. For example:

// Querying users in the dsMysql data source
List<Record> users = Db.use("mysql").find("select * from user");
// Querying blogs in the dsOracle data source
List<Record> blogs = Db.use("oracle").find("select * from blog");
1
2
3
4

In the above two lines of code, database operation objects are obtained for their respective data sources (with configNames "mysql" and "oracle"). After that, you can use the database operation APIs just as you would with a single data source. In other words, for Db + Record, multiple data sources only require an additional call to Db.use(configName), and subsequent API usage remains the same.

Note: The first created ActiveRecordPlugin instance will become the primary data source and can have its configName omitted. The primary instance's configurations will be the default settings. You can also set the primary configuration by setting the configName to DbKit.MAIN_CONFIG_NAME.

Last Updated: 9/21/2023, 8:42:09 AM